<template>
  <div style="height: 6vh;background-color:#fff;padding: 10px 20px;">
    <span style="line-height:60px;font-size:20px;">审批流程</span>
    <el-button type="primary" style="float: right;margin-top: 10px;"
               @click="beforeAddUser">添加审批流程类型</el-button>
  </div>
  <!-- 新建流程弹窗 -->
  <!-- :before-close="handleClose" 在弹窗关闭前先调用handleClose方法来处理弹窗的关闭问题
   避免点击弹窗外部也能关闭弹窗-->
  <el-dialog
      :title="dialogTitle"
      style="width: 1000px;padding: 40px;"
      v-model="dialogVisible"
      :before-close="handleClose"
  >
    <el-form label-position="top" label-width="80px">
      <el-row :gutter="10">
        <el-col :span="12">
          <el-form-item label="流程名称">
            <el-input placeholder="请输入" v-model="saveApprovalForm.name"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="流程类型">
            <el-select placeholder="请选择" v-model="saveApprovalForm.processType"  @change="loadApproval">
              <el-option v-for="item in approvalProcessArr"
                         :label="item.label" :value="item.value"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="10">
        <el-col :span="12">
          <el-form-item label="应用模块">
            <el-select placeholder="请选择" v-model="saveApprovalForm.applicationModule" @change="loadApproval">
              <el-option v-for="item in moduleTypeArr "
                         :label="item.label" :value="item.value"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="审批类型">
            <el-select placeholder="请选择" v-model="saveApprovalForm.approvalType" @change="loadApproval">
              <el-option v-for="item in approvalTypeArr"
                         :label="item.label" :value="item.value"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="10">
        <el-col :span="12">
          <el-form-item label="应用部门">
            <el-select placeholder="请选择" v-model="saveApprovalForm.applicableDepartment" @change="loadApproval">
              <el-option v-for="item in departmentTypeArr"
                         :label="item.label" :value="item.value"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="状态">
            <el-radio-group v-model="saveApprovalForm.status">
              <el-radio border label="启用" value="1" style="margin:0;"></el-radio>
              <el-radio border label="禁用" value="0"></el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <template #footer>
      <el-button @click="handleClose">取消</el-button>
      <el-button type="primary" @click="saveUser">保存</el-button>
    </template>
  </el-dialog>

  <!-- 审批流程卡片 -->
  <el-card style="margin: 20px;">
    <el-table :data="approvalArr">
      <el-table-column type="index" label="编号" width="80" align="center"></el-table-column>
      <el-table-column prop="name" label="名称" align="center"></el-table-column>
      <el-table-column prop="processType" label="流程类型" align="center"
                       :formatter="approvalProcessFormatter "></el-table-column>
      <el-table-column prop="applicationModule" label="应用模块" align="center"
                       :formatter="moduleTypeFormatter"></el-table-column>
      <el-table-column prop="approvalType" label="审批类型" align="center"
                       :formatter="approvalTypeFormatter"></el-table-column>
      <el-table-column prop="applicableDepartment" label="适用部门" align="center"
                       :formatter="departmentTypeFormatter"></el-table-column>
      <el-table-column prop="status" label="状态" align="center" :formatter="statusTypeFormatter"></el-table-column>
      <el-table-column label="操作" align="center">
        <template #default="scope">
          <el-button link type="primary" @click="editApprovalProces(scope.row.id)">编辑</el-button>
          <el-button link type="primary" @click="deleteApprovalProces(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
  </el-card>
</template>

<script setup>
import {onMounted, ref} from "vue";
import axios from "axios";
import qs from "qs";
import {ElMessage} from "element-plus";

//定义变量控制弹窗是否出现
const dialogVisible = ref(false);
//定义变量用来控制弹窗的标题
const dialogTitle = ref('新建审批流程');
// 用户列表
const users = ref(getUser());
//定义对象用来收集保存表单的数据
const saveApprovalForm = ref({
  name:'',
  processType:'',
  applicationModule:'',
  approvalType:'',
  applicableDepartment:'',
  status:'',
  processAdmin:'',
});
/* 点击"新建"按钮,出弹窗+修改弹窗标题 */
const beforeAddUser = ()=>{
  dialogVisible.value = true;
  dialogTitle.value = '新建审批流程';
}
//定义保存用户的方法
const saveUser = ()=>{
  console.log(saveApprovalForm.value);
  /* 安装查询字符串框架qs  npm install qs
  使用qs框架将JS对象转为查询字符串格式 name=tom&age=18,注意qs需要导入!!! import qs from "qs"; */
  let data = qs.stringify(saveApprovalForm.value);
  console.log(data);
  //向后端发送保存员工的请求
  /* 此处传的参数是qs转换后的查询字符串data,注意:使用了查询字符串,后端Controller不用加@RequestBody注解!!! */
  axios.post(BASE_URL+'/oa1/approval/save',data)
      .then((response)=>{
        if(response.data.code == 2000){
          ElMessage.success('保存成功!');
          //关闭弹窗,并清空双向绑定的表单数据
          dialogVisible.value = false;
          saveApprovalForm.value = {};
          loadApproval();
          //保存成功后要重新加载用户数据!
        }else{
          ElMessage.error(response.data.msg);
        }
      })
}
//处理弹窗关闭的方法
const handleClose = ()=>{
  if(confirm('您确认要关闭本窗口吗?')){
    //关闭弹窗,并清空双向绑定的表单数据
    dialogVisible.value = false;
    saveApprovalForm.value = {};
  }
}

//定义对象用来保存查询条件
const searchApprovalForm = ref({approvalModule:'', status:'',name:''});
//定义对象保存查询到的表格数据
const approvalArr = ref([]);
//定义加载用户的方法
const loadApproval = ()=>{
  let data = qs.stringify(searchApprovalForm.value);//username=tom&status=1
  //如果有查询条件,就根据后面拼接的条件查,没有条件默认加载所有用户数据
  axios.get(BASE_URL+'/oa1/approval/select?'+data)
      .then((response)=>{//回调函数中处理响应结果
        if(response.data.code == 2000){
          approvalArr.value = response.data.data;//将后端响应数据装入用户数组中
          console.log(approvalArr.value);
        }else{
          ElMessage.error(response.data.msg);
        }
      })
}
//定义重置搜索条件的方法
const resetSearch = ()=>{
  //清空之前的搜索条件
  searchApprovalForm.value = {};
  //重新加载所有的用户数据
  loadApproval();
}

/* 页面一加载完毕,立即执行 */
onMounted(()=>{
  loadApproval()
})

//创建修改审批流程的选项
//创建删除员工的方法
const deleteApprovalProces = (id)=>{
  if(confirm('您确认要删除该数据吗?')){
    axios.post(BASE_URL+'/oa1/approval/delete/'+id)
        .then((response)=>{
          if(response.data.code == 2000){
            ElMessage.success('删除成功!');
            loadApproval();
          }else{
            ElMessage.error(response.data.msg);
          }
        })
  }
}
//定义部门类型列表
const departmentTypeArr = ref([]);
const departmentTypeFormatter = (row,column,cellValue,index)=>{
  if(!departmentTypeArr.value){
    return cellValue;
  }
  for(let item of departmentTypeArr.value){
    if(item.value == cellValue){
      cellValue = item.label;
    }
  }
  return cellValue;
}
//定义审批分类
const approvalTypeArr = ref([]);
const approvalTypeFormatter = (row,column,cellValue,index)=>{
  if(!approvalTypeArr.value){
    return cellValue;
  }
  for(let item of approvalTypeArr.value){
    if(item.value == cellValue){
      cellValue = item.label;
    }
  }
  return cellValue;
}
//定义状态
const statusTypeArr = ref([]);
const statusTypeFormatter = (row,column,cellValue,index)=>{
  if(!statusTypeArr.value){
    return cellValue;
  }
  for(let item of statusTypeArr.value){
    if(item.value == cellValue){
      cellValue = item.label;
    }
  }
  return cellValue;
}
//定义模块
const moduleTypeArr = ref([]);
const moduleTypeFormatter = (row,column,cellValue,index)=>{
  if(!moduleTypeArr.value){
    return cellValue;
  }
  for(let item of moduleTypeArr.value){
    if(item.value == cellValue){
      cellValue = item.label;
    }
  }
  return cellValue;
}
const approvalProcessArr = ref([]);
const approvalProcessFormatter = (row,column,cellValue,index)=>{
  if(!moduleTypeArr.value){
    return cellValue;
  }
  for(let item of approvalProcessArr.value){
    if(item.value == cellValue){
      cellValue = item.label;
    }
  }
  return cellValue;
}

onMounted(()=>{
  loadDictOption(departmentTypeArr,'department_type');
  loadDictOption(approvalTypeArr,'approval_type');
  loadDictOption(statusTypeArr,'general_status');
  loadDictOption(moduleTypeArr,'module');
  loadDictOption(approvalProcessArr,'approval_process');
})
const editApprovalProces = (id)=>{
  dialogVisible.value = true;
  dialogTitle.value = '修改审批流程';
  axios.get(BASE_URL+'/oa1/approval/select?id='+id).then((response)=>{
    if(response.data.code == 2000){
      saveApprovalForm.value = response.data.data[0];
      dialogVisible.value = true;
    }else{
      ElMessage.error(response.data.msg);
    }
  })
}



</script>

<style scoped>
</style>